﻿using System;
using System.Collections.Generic;
using IntegratedMeasuring.Utils;
using Newtonsoft.Json;
using SqlSugar;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace IntegratedMeasuring.ViewModels.Scheme;

public class TestRecord : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler? PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    [SugarColumn(IsPrimaryKey = true)]
    public long Id { get; set; }

    public long ProductId { get; set; }

    public int Pri { get; set; }
    public int Sec { get; set; }
    public string MinorTerm { get; set; } = string.Empty;

    public string Phase { get; set; } = string.Empty;
    [SugarColumn(IsNullable = true)]
    public string? Unit { get; set; }
    [SugarColumn(IsNullable = true)]
    public string? DetectionValue { get; set; }
    
    [SugarColumn(IsNullable = true)] public bool IsSynch { get; set; } = false;

    private string? _displayDetectionValue;
    [SugarColumn(IsNullable = true)]
    public string? DisplayDetectionValue
    {
        get => _displayDetectionValue;
        set
        {
            if (_displayDetectionValue != value)
            {
                _displayDetectionValue = value;
                OnPropertyChanged();
            }
        }
    }

    private string? _displayUnit;
    [SugarColumn(IsNullable = true)]
    public string? DisplayUnit
    {
        get => _displayUnit;
        set
        {
            if (_displayUnit != value)
            {
                _displayUnit = value;
                OnPropertyChanged();
            }
        }
    }

    private int _result = 2;
    public int Result
    {
        get => _result;
        set
        {
            if (_result != value)
            {
                _result = value;
                OnPropertyChanged();
            }
        }
    }
}



public class ProductInfo
{
    [SugarColumn(IsIgnore = true)]
    public int index { get; set; } = 0; // 产品在工单中的序号
    [SugarColumn(IsPrimaryKey = true)]
    public long Id { get; set; }
    [SugarColumn(IsNullable = true)]
    public int SerialNumber { get; set; } // 测试序号(一个条码被测多测，记录每次的序号)
    public long ConditionId { get; set; } // 工单表的id,  可关联到工单表的唯一记录
    [SugarColumn(IsNullable = true)]
    public string BarCode { get; set; } = string.Empty;
    public int Result { get; set; } = 2;
    [JsonConverter(typeof(CustomDateTimeConverter))]
    public DateTime StartTime { get; set; }
    [JsonConverter(typeof(CustomDateTimeConverter))]
    public DateTime EndTime { get; set; }


    [SugarColumn(IsIgnore = true)]
    public string OrderName { get; set; } = string.Empty;
    [SugarColumn(IsIgnore = true)]
    public string TestCondition { get; set; } = string.Empty;
    [SugarColumn(IsIgnore = true)]
    public string BatchNumber { get; set; } = string.Empty;

    [SugarColumn(IsNullable = true)] public bool IsSynch { get; set; } = false;

    [SugarColumn(IsIgnore = true)]
    public List<TestRecord> TestDataDetail { get; set; } = new List<TestRecord>();

    public ProductInfo()
    {
        StartTime = DateTime.Now;
        EndTime = DateTime.Now;
    }
}